
set IRLEN 8

# 一些命令
set IDCODE 0x06
set JTAG_PROGRAM 0x30
set SPI_PROGRAM  0x39
set JTAG_START   0x3d
set BYPASS 0xff
set REFRESH 0x1

# 创建DAP
jtag newtap EG4S20 tap -irlen $IRLEN -expected-id 0x0a014c35

init

# 获得IDCODE, 分低16位与高16位
irscan EG4S20.tap $IDCODE -endstate RUN/IDLE
set val [drscan EG4S20.tap 16 0x00000000 -endstate DRPAUSE]
puts ${val}
set val [drscan EG4S20.tap 16 0x00000000 -endstate RUN/IDLE]
puts 6666
puts ${val}

source data_proc.tcl

#未实现
proc fpga-sram {args} {
    # 需要设备处理bypass指令
    irscan EG4S20.tap $::BYPASS -endstate RUN/IDLE
    irscan EG4S20.tap $::BYPASS -endstate RUN/IDLE

    irscan EG4S20.tap $::REFRESH -endstate RUN/IDLE

    #加载
    irscan EG4S20.tap $::BYPASS -endstate RUN/IDLE
}

proc fpga-flash-start {} {
    # 执行五次$BYPASS ?
    irscan EG4S20.tap $::BYPASS -endstate RUN/IDLE
    irscan EG4S20.tap $::BYPASS -endstate RUN/IDLE
    irscan EG4S20.tap $::BYPASS -endstate RUN/IDLE
    irscan EG4S20.tap $::BYPASS -endstate RUN/IDLE
    irscan EG4S20.tap $::BYPASS -endstate RUN/IDLE
    # 0a014c35
    irscan EG4S20.tap $::REFRESH -endstate RUN/IDLE

    # 处理 bypass & spi_program 指令
    irscan EG4S20.tap $::BYPASS -endstate RUN/IDLE
    irscan EG4S20.tap $::SPI_PROGRAM -endstate RUN/IDLE

    # 等待时钟
    runtest 10000
    runtest 10000
    runtest 10000
}


proc fpga-reset {} {
    irscan EG4S20.tap $::BYPASS -endstate RUN/IDLE
    irscan EG4S20.tap $::REFRESH -endstate RUN/IDLE
    runtest 100
    irscan EG4S20.tap $::BYPASS -endstate RUN/IDLE
    runtest 10000
}

proc sflash_send_end {data len} {
    set val [sflash_send32 $data RUN/IDLE]
    return $val;
}

proc sflash_cmd {cmd addr len} {
    set send_data [format "0x%x" [expr {[toMSB $cmd]+([toMSB24 $addr]<<8)}]];
    # puts $send_data
    set val [drscan EG4S20.tap $len $send_data -endstate RUN/IDLE]
    return $val;
}

# 与sflash_cmd 不同的是,cmd结束后停留在DRSHIFT
proc sflash_cmd_cc {cmd addr len} {
    set send_data [format "0x%x" [expr {[toMSB $cmd]+([toMSB24 $addr]<<8)}]];
    # puts "$len $send_data"
    set val [drscan EG4S20.tap $len $send_data -endstate DRSHIFT]
    return $val;
}


set FLASH_RDSFDP 0x5a
set FLASH_READ 0x03

fpga-flash-start

#---------------------------------------------------------------
#输出flash SFDP
set vv  [sflash_cmd $::FLASH_RDSFDP 0x0 128]
set vv2  [sflash_cmd $::FLASH_RDSFDP 0x0 128]
if {0} {
    # 44c595ffffffffff 是正确的. 需要转为 2进制.需要偏移33位 (舍弃一位经过jtag读取)
    puts $vv
    puts $vv2
    # 转换成字节
    set arr [hex_to_byte $vv 5]
    puts $arr
} else {
    set arr [hex_bit_shift $vv 1]
    set arr [hex_to_byte $arr 5]
    puts $arr
    foreach b $arr {
        set hex [format "%02x" $b]
        puts $hex;
    }
}
#结束输出flash SFDP
#------------------------------------------------------------
#
set vv [sflash_cmd $::FLASH_READ 0x0 128]
puts "read data!"
puts $vv
set arr [hex_bit_shift $vv 1]
set arr [hex_to_byte $arr 5]
foreach b $arr {
    set hex [format "%02x" $b]
    puts $hex;
}

proc sflash_send512_MSB {data {endstate DRSHIFT} } {
    set val [drscan EG4S20.tap 512 $data -endstate $endstate]
    return $val
}

proc sflash_send32 {data {endstate DRSHIFT} } {
    set hex0 [toMSB [expr {($data>>0)&0xff}]];
    set hex1 [toMSB [expr {($data>>8)&0xff}]];
    set hex2 [toMSB [expr {($data>>16)&0xff}]];
    set hex3 [toMSB [expr {($data>>24)&0xff}]];

    set send_data [format "0x%02x%02x%02x%02x" $hex3 $hex2 $hex1 $hex0]
    # puts "s:$send_data"
    # set val [drscan EG4S20.tap 32 $send_data -endstate DRSHIFT]
    set val [drscan EG4S20.tap 32 $send_data -endstate $endstate]
    return $val;
}

proc sflash_program_begin {addr} {
    # 0x6命令开启编程
    sflash_cmd 0x6 0x0 8
    runtest 100
    # puts "status begin:[hex_to_byte [sflash_cmd 0x5 0x0 40] 4]"
    sflash_cmd_cc 0x2 $addr 32
}

proc sflash_program_end {end_dat} {
    # pathmove RUN/IDLE
    sflash_send_end $end_dat 32
    runtest 1000
    sflash_cmd 0x4 0x0 8
    # 0x4关闭编程
    # puts "status end:[hex_to_byte [sflash_cmd 0x5 0x0 40] 4]"
}